package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.ExamMapper;
import com.cqjtu.drive.pojo.Exam;
import com.cqjtu.drive.pojo.Reservation;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Override
    public Response<Exam> getExamById(String eId) {
        try {
            Exam exam = examMapper.getExamById(eId);
            if (exam != null) {
                return new Response<>(true, "Exam found", exam);
            } else {
                return new Response<>(false, "Exam not found");
            }
        } catch (Exception e) {
            return new Response<>(false, "Error retrieving exam: " + e.getMessage());
        }
    }

    @Override
    public Response<String> insertExam(Exam exam) {
        try {
            List<Exam> list = examMapper.getAllExams();
            if(list != null && !list.isEmpty()){
                String id = Integer.toString(Integer.parseInt(list.get(list.size() - 1).geteId()) + 1);
                exam.seteId(id);
            }else {
                exam.seteId("1");
            }
            examMapper.insertExam(exam);
            return new Response<>(true, "Exam inserted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Error inserting exam: " + e.getMessage());
        }
    }

    @Override
    public Response<String> updateExam(Exam exam) {
        try {
            examMapper.updateExam(exam);
            return new Response<>(true, "Exam updated successfully");
        } catch (Exception e) {
            return new Response<>(false, "Error updating exam: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteExam(String eId) {
        try {
            examMapper.deleteExam(eId);
            return new Response<>(true, "Exam deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Error deleting exam: " + e.getMessage());
        }
    }

    @Override
    public Response<List<Exam>> getAllExams() {
        try {
            List<Exam> exams = examMapper.getAllExams();
            return new Response<>(true, "Exams retrieved successfully", exams);
        } catch (Exception e) {
            return new Response<>(false, "Error retrieving exams: " + e.getMessage());
        }
    }
}
